﻿#region Version Info
/* ========================================================================
* 【本类功能概述】
* 
* 作者：李荣楠
* 时间：2013-10-29 15:58:50
* 文件名：BasePage
* .net版本：4.0 
* ========================================================================
*/
#endregion
using System;
using System.Web;
using LCL.Common;
using LCL.Config;
using LCL.Entity;
using NVelocity;
namespace LCL.MVC
{
    public class BasePage:IHandlerFactory
    {
        #region 变量
        /// <summary>
        /// 页面后缀
        /// </summary>
        private string ExName = UiConfig.ExName;
        /// <summary>
        /// 站点虚拟目录
        /// </summary>
        protected  string root = HttpContext.Current.Request.ApplicationPath;
        /// <summary>
        /// 用户id
        /// </summary>
        protected string uID = "0";
        #endregion

        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        public BasePage()
        {
            if (root == "/") root = string.Empty;
            string isLogin = UiConfig.IsLogin;
            if (isLogin == "0" && this.GetUserID() == "0")
            {
                //if中的条件表示这些页面在不登陆的时候可以访问
                if (HttpContext.Current.Request.Url.ToString().ToLower().IndexOf("/login") == -1
                    && HttpContext.Current.Request.Url.ToString().ToLower().IndexOf(root+"/index.html") == -1)
                {
                    HttpContext.Current.Response.Redirect(root + "/login" + ExName + "?urls=" + HttpContext.Current.Request.Url);
                }
            }
        }
        #endregion

        #region IHandlerFactory 成员

        public virtual void Page_Load(ref NVelocity.VelocityContext context)
        {
            
        }

        public virtual void Page_PostBack(ref NVelocity.VelocityContext context)
        {
           
        }

        #endregion

        #region 方法
        /// <summary>
        /// 页面加载预处理
        /// </summary>
        /// <param name="context"></param>
        private void InitLoad(ref VelocityContext context)
        {
            //获取用户id
            uID  = GetUserID();
            context.Put("uid", uID);

            if (uID!= "0")
            {
                //用户已登录

            }
            else
            {
                //用户未登录

            }
          
        }

        /// <summary>
        /// 取得当前登录ID
        /// </summary>
        /// <returns></returns>
        protected string GetUserID()
        {
            string uID = "0";
            string cookiesFlag = UiConfig.UsersCookiesFlag;
            if (HttpContext.Current.Request.Url.ToString().IndexOf("/admin/") > -1)
            {
                cookiesFlag = UiConfig.AdminCookiesFlag;
            }
            HttpCookie cookieToken = HttpContext.Current.Request.Cookies[cookiesFlag];
            if (cookieToken == null || cookieToken["token"] == null || cookieToken["token"] == "")
                return "0";
            string userCookie = cookieToken["token"];
            string desstr = Input.Decrypt(userCookie, SecretConfig.UserKey, SecretConfig.UserIV);//解密后是一个字符串，包括4个部分，使用“&”分隔
            string[] userInfo = desstr.Split(SecretConfig.UserSeparator);//将四部分存入数组，分别是用户id，密码，ip，
            EnumLoginState state = EnumLoginState.Err_TimeOut;
            if (userInfo.Length == 4)
            {
                uID = userInfo[0];
                string pass = userInfo[1];
                string cookip = userInfo[2];
                string cookevcode = userInfo[3];
                //加密验证码
                if (cookevcode != Input.MD5(UiConfig.CookieVerifyCode, false))
                {
                    return "0";
                }
                if (HttpContext.Current.Request.Url.ToString().IndexOf("/admin/") > -1)
                {
                    //判断管理员登录
                    //state = Match.Home.user.user.Instance.CheckUser(uID, pass, true);
                }
                else
                {
                    //state = Match.Home.user.user.Instance.CheckUser(uID, pass, false);
                }
            }
            if (state == EnumLoginState.Succeed)
            {
                return uID;
            }
            else
            {
                return "0";
            }
        }
        #endregion
    }
}
